package com.groupseven.backend.controller;

import com.groupseven.backend.domain.Permission.PermissionItem;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.services.AuthoritiesService;
import com.groupseven.backend.services.UserService;
import com.groupseven.backend.test.DataEncode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/Authorities" )
public class AuthoritiesController {

    @Autowired
    private AuthoritiesService authoritiesService;

    @Autowired
    private UserService userService;

    @Autowired
    private DataEncode dataEncode;



    /**
     * @Author 王志华
     * @param Username
     * @return
     */
    @GetMapping("/{Username}")
    public ResultType getMyPermissions(@PathVariable String Username){
        User user = new User();
        Username = dataEncode.encodeBae64(Username);
        User u1 = userService.selectByUsername(Username);
        System.out.println("getPermissions"+Username);
        user.setUsername(Username);
        Set<String> permissions = authoritiesService.getPermissionsByUsertype(u1.getUsertype());
        if (permissions != null){
            return new ResultType(1,"查询成功！",200,permissions);
        }
        return new ResultType(0,"查询失败！",200,permissions);
    }


    @GetMapping("/permissiontree/{Usertype}")
    public ResultType getMyPermissionTree(@PathVariable int Usertype){
        ResultType result = new ResultType(1,"查询成功",200);
        List<PermissionItem> roots = authoritiesService.getParentPermissions();
        List<PermissionItem> subs = authoritiesService.getSubPermissions();
        List<PermissionItem> checkedroots = authoritiesService.getParentPermissiontreeByRolenumber(Usertype);
        List<PermissionItem> checkedsubs = authoritiesService.getSubPermissiontreeByRolenumber(Usertype);
        List<Integer> checked = new ArrayList<>();
        for(PermissionItem rootIterator : roots){
            List<PermissionItem> temp = new ArrayList<>();
            for(PermissionItem subIterator : subs){
                if(rootIterator.getAuthoritiesNumber()==subIterator.getParentNumber()){
                    temp.add(subIterator);
                }
            }
            if(temp.size()!=0){
                rootIterator.setSubs(temp);
            }
        }
        for(PermissionItem rootIterator : checkedroots){
            checked.add(rootIterator.getAuthoritiesNumber());
            List<PermissionItem> temp = new ArrayList<>();
            for(PermissionItem subIterator : checkedsubs){
                if (checked.contains(subIterator.getAuthoritiesNumber())){

                } else {
                    checked.add(subIterator.getAuthoritiesNumber());
                }
                if(rootIterator.getAuthoritiesNumber()==subIterator.getParentNumber()){
                    temp.add(subIterator);
                }
            }
            if(temp.size()!=0){
                rootIterator.setSubs(temp);
            }
        }
        result.setPermissions(checked);
        result.setPermissionTree(roots);
        return result;
    }

    @PutMapping("/{Usertype}")
    public ResultType updatePermissionsByRole(@PathVariable Integer Usertype,@RequestParam int[] Permissions){
        System.out.println(Usertype);
        System.out.println(Permissions);
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("Usertype",Usertype);
        query.put("Permissions",Permissions);
        System.out.println(query.get("Usertype"));
        System.out.println(query.get("Permissions").toString());
        return new ResultType(authoritiesService.updateRolePermissions(Usertype,query),"修改成功！",200);
    }
}
